package com.ihr360.cnb.module.insurance.controller;

import com.ihr360.cnb.module.insurance.service.MemberPlanAggregateService;
import com.ihr360.cnb.module.insurance.service.feign.InsuranceService;
import com.ihr360.cnb.module.payroll.service.PayrollAggregateService;
import com.ihr360.commons.data.util.Pair;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.lang.StringUtils;
import com.ihr360.commons.model.type.SIHFLedgerState;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.request.ApplicationApprovalRequest;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Eric
 */
@RestController
@RequestMapping("/ledgerSummary")
@DisableResponseAdvice
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.STANDING_BOOK_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.STANDING_BOOK_NAME)
public class LedgerSummaryAggregateController {


    @Autowired
    private PayrollAggregateService payrollAggregateService;

    @Autowired
    private MemberPlanAggregateService memberPlanAggregateService;

    @Autowired
    private InsuranceService insuranceService;

    /**
     * 福利提请审批
     */
    @FunctionDefinition(functionName = "福利提请审批", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "/submit/approval", httpMethod = "POST", notes = "福利提请审批")
    @RequestMapping(value = "/submit/approval", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void submitApproval(AuthUserInfo userInfo, @RequestBody ApplicationApprovalRequest applicationApprovalRequest) {
        if (applicationApprovalRequest == null || StringUtils.isEmpty(applicationApprovalRequest.getApplicationTitle())
                || StringUtils.isEmpty(applicationApprovalRequest.getType()) || applicationApprovalRequest.getId() == null) {
            throw new Ihr360Exception(null, "提请审批信息不可为空");
        }
        applicationApprovalRequest.setCompanyId(userInfo.getCompanyId());
        applicationApprovalRequest.setMobileNo(userInfo.getMobileNo());
        payrollAggregateService.submitApproval(userInfo, applicationApprovalRequest);
    }

    /**
     * 福利审批同步状态
     */
    @FunctionDefinition(functionName = "福利审批同步状态", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "/approval/state", httpMethod = "GET", notes = "福利审批同步状态")
    @RequestMapping(value = "/approval/state", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void updateApprovalState(@RequestParam String companyId, @RequestParam String applicationId, @RequestParam SIHFLedgerState state) {
        if (state == null || StringUtils.isBlank(applicationId)) {
            throw new Ihr360Exception(null, "提请审批信息不可为空");
        }
        insuranceService.updateApprovalState(companyId, applicationId, state);
        if (SIHFLedgerState.CLOSED.equals(state)) {
            Pair<Long, String> idUserIdPair = insuranceService.getLedgerSummaryIdUserId(companyId, applicationId);
            if (idUserIdPair != null) {
                Long id = idUserIdPair.getFirst();
                String userId = idUserIdPair.getSecond();
                // TODO: 18-5-24 执行失败，补偿处理
                memberPlanAggregateService.saveIncreaseMemberRecords(companyId, userId, id);
                memberPlanAggregateService.saveDecreaseMemberRecords(companyId, userId, id);
            }
        }
    }
}
